/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihomeapp;

import be.contribute.common.pojo.Temperature;
import be.contribute.pihomeapp.info.ChartDataProvider;
import be.contribute.pihomeapp.ws.HomeClient;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author wimvandecasteele
 */
public class PiHomeControlController implements Initializable {
    
    private HomeClient client = new HomeClient("http://192.168.0.234:8080/PiHome");   
    
    @FXML
    private LineChart<String, Number> lineChart;
    @FXML
    private Label lblConnection;
    @FXML
    private Label lblTemp1;
    @FXML
    private Label lblHum1;    
    @FXML
    private Label lblTime1;
    @FXML
    private Label lblTemp2;
    @FXML
    private Label lblHum2;    
    @FXML
    private Label lblTime2;
    @FXML
    private ImageView imgCam;
    
    private void populateChart(List<Temperature> pListRoom1, List<Temperature> pListRoom2)
    {
        ObservableList<XYChart.Series<Date, Number>> lineChartData = FXCollections.observableArrayList();
        lineChart.setTitle(new Date().toString());        
        HashMap hm = new HashMap();
       
        hm.put("Living room",ChartDataProvider.convertTemperatureListToTempValueList(pListRoom1,20));
        hm.put("Bathroom",ChartDataProvider.convertTemperatureListToTempValueList(pListRoom2,20));
                
        List<XYChart.Series> vSeries = ChartDataProvider.getDayChart(hm, 0);
         for(XYChart.Series vSerie: vSeries)
        {
            lineChart.getData().add(vSerie);
        }
    }
    
    @FXML
    private void refereshRoom1(ActionEvent event) {
        Temperature temp = client.getRoomTemperature(1);
        refreshRoomInfo(1, temp);
    }
    
    @FXML
    private void openRoom1Tab() {
        Temperature temp = client.getRoomTemperature(1);
        refreshRoomInfo(1, temp);
    }
    
    @FXML
    private void refereshRoom2(ActionEvent event) {
        Temperature temp = client.getRoomTemperature(2);
        refreshRoomInfo(2, temp);
    }
    
    @FXML
    private void openRoom2Tab() {
        Temperature temp = client.getRoomTemperature(2);
        refreshRoomInfo(2, temp);
    }
    
    @FXML
    private void aircoOn(ActionEvent event) {
        client.controlAirco(1, 1);
    }
    
    @FXML
    private void heaterOn(ActionEvent event) {
        client.controlHeater(1, 1);
    }
    
    @FXML
    private void aircoOff(ActionEvent event) {
        client.controlAirco(1, 0);
    }
    
    @FXML
    private void heaterOff(ActionEvent event) {
        client.controlHeater(1, 0);
    }
    
    private void refreshRoomInfo(int pRoomId,Temperature pTemp)
    {
        if(pRoomId == 1)
        {
            lblTemp1.setText(pTemp.getTempValue() + " °C");
            lblHum1.setText(pTemp.getHumValue() + " %");
            lblTime1.setText(pTemp.getTime().toString());
        }
        if(pRoomId == 2)
        {
            lblTemp2.setText(pTemp.getTempValue() + " °C");
            lblHum2.setText(pTemp.getHumValue() + " %");
            lblTime2.setText(pTemp.getTime().toString());
        }
    }
    
    @FXML
    private void refreshChart()
    {
        List<Temperature> vListRoom1 = client.getRoomTemperatures(1);
        List<Temperature> vListRoom2 = client.getRoomTemperatures(2);

       populateChart(vListRoom1, vListRoom2);
    }
    
    @FXML
    private void refreshCam()
    {
        String vUrl = client.controlCamera();
        if(vUrl != null)
        {   
            String vRealUrl = vUrl.substring(vUrl.indexOf("src=") + 5);
            vUrl = vRealUrl.substring(0,vRealUrl.indexOf(".jpg") + 4);
            Image img = new Image(vUrl);
            System.out.println("Img URL: " + vUrl);
            imgCam.setImage(img);
        }
        
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
       String vTest = client.getIntro();
       if(vTest != null && vTest.length() > 10)
       {
           lblConnection.setText("OK");
       }
       else
       {
           lblConnection.setText("ERROR");
       }
    }    
}
